package com.hdoit.apollo.web.billing.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.apollo.logical.billing.mapper.BillingMapper;
import com.hdoit.apollo.logical.billing.model.Billing;
import com.hdoit.apollo.logical.billing.service.IBillingService;
import com.hdoit.apollo.logical.shop.model.Shop;
import com.hdoit.apollo.logical.shop.model.Staff;
import com.hdoit.apollo.logical.shop.service.IStaffService;
import com.hdoit.apollo.logical.viewmodel.billing.BillingSearch;
import com.hdoit.apollo.logical.viewmodel.stock.ReturnCargoInfo;
import com.tennetcn.authority.component.web.ActionResult;
import com.tennetcn.authority.component.web.BaseController;
import com.tennetcn.authority.component.web.ErrorResult;
import com.tennetcn.authority.component.web.JsonActionResult;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.message.ManageContainer;
import com.tennetcn.core.common.utils.CommonUtils;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.core.common.utils.JsonUtils;
import com.tennetcn.data.enums.ModelStatus;
import com.tennetcn.data.message.PagerModel;
import com.tennetcn.web.message.WebManageContainer;

/**
 * @author      tancongzhu
 * @email       
 * @createtime  2018年04月26日 11:04:02
 * @comment
 */
@Controller
@RequestMapping(value = "/billing/returnCargo/")
public class ReturnCargoController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/billing/returnCargo/";
    }

    @Autowired
    private IBillingService billingService;
    
    @Autowired
    private BillingMapper billingMapper;
	
	@Autowired
	private IStaffService staffService;

    private final String indexViewName="index";
    private final String outputViewName="output";
    private final String returnCargoDetailed = "returnCargoDetailed";
    private final String returnCargoSave="returnCargoSave";
    private final String deleteViewName = "delete";
    private final String detailedPrint="detailedPrint";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,BillingSearch search,boolean ajaxPost){
    	search.setType("returnCargo");
    	
        pagerModel.setTotalAmount(billingMapper.queryCountMPForPc(search));
        List<Billing> billings= billingMapper.queryListMPForPc(search,pagerModel);

        model.addAttribute(WebManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("billings", billings);
        model.addAttribute("billingSearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = returnCargoDetailed, method = { RequestMethod.GET})
    public String returnCargoDetailed(Model model,String key){
        Billing billing=new Billing();
        if(!StringUtils.isEmpty(key)){
        	billing =billingService.getBillingByKey(key);
        }

        model.addAttribute("billingJson", JsonUtils.writeValueAsString(billing));
        model.addAttribute("billing", billing);
        return view(returnCargoDetailed);
    }
    
    @RequestMapping(value = returnCargoSave, method = { RequestMethod.POST})
    public String returnCargoSave(Model model,Billing billing,ReturnCargoInfo returnCargoInfo){
    	String timestamp = DateUtils.convertDate2String("yyyyMMddHHmmss", DateUtils.getCurrent());
        if(StringUtils.isEmpty(billing.getBillingId())){
        	billing.setBillingNo(timestamp);
            billing.setBillingId(CommonUtils.getKeyId());
            billing.setCreateDate(DateUtils.getCurrent());
            billing.setDeleteMark(YesOrNoInteger.no);
            billing.setCreateUserId(getLoginModel().getLoginId());
            billing.setCreateUserName(getLoginModel().getLoginName());
            billing.setModelStatus(ModelStatus.add);
            Shop shop=(Shop)webSessionFactory.getCurrentLogin().getArgument("shop");
            billing.setShopId(shop.getShopId());
        }else{
            billing.setModifyDate(DateUtils.getCurrent());
            billing.setModifyUserId(getLoginModel().getLoginId());
            billing.setModifyUserName(getLoginModel().getLoginName());
            billing.setModelStatus(ModelStatus.update);
        }
        Staff staff=staffService.queryModel(billing.getBusinessId());
        if(staff!=null) {
        	billing.setBusinessName(staff.getName());
        }
        System.err.println(returnCargoInfo.getRealPrice());
        System.err.println(returnCargoInfo.getCollocationDebt());
        ActionResult actionResult;
        if(billingService.applyBillingAndReturnCargo(billing,returnCargoInfo)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("billing", billing);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(returnCargoDetailed);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(billingService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
    
    @RequestMapping(value = detailedPrint, method = { RequestMethod.GET})
    public String detailedPrint(Model model,String key){
        Billing billing=new Billing();
        if(!StringUtils.isEmpty(key)){
        	billing =billingService.getBillingByKey(key);
        	
        }
        model.addAttribute("billing", billing);
        return view(detailedPrint);
    }
}
